#pragma once

#include "../core/ApplicationContext.h"
#include <includeBase.h>
#include "../utils/QtUtil.h"

class BaseAction : public QAction
{
protected:
    shared_ptr<ApplicationContext> context;
    QMainWindow* mainWindow;

public:
    BaseAction(
        shared_ptr<ApplicationContext> context,
        const QString& name,
        const QString& text,
        QKeySequence* shortcut,
        QIcon* icon
    )
        : QAction(context->mainWindow())
    {
        this->context = context;
        this->mainWindow = context->mainWindow();
        this->setObjectName(name);
        this->setText(text);
        if (shortcut) {
            this->setShortcut(*shortcut);
        }
        if (icon) {
            this->setIcon(*icon);
        }
        connect(this, &QAction::triggered, this, &BaseAction::doAction);
    }

    void showMe()
    {
        QMessageBox::information(
            this->mainWindow, context->ProductName, this->text()
        );
    }

    virtual void execute() { showMe(); }

    void showErrorMessageBox(std::exception& e)
    {
        QMessageBox::critical(this->mainWindow, context->ProductName, e.what());
    }

    void showErrorMessageBox(const QString& message)
    {
        QMessageBox::critical(
            context->mainWindow(), context->ProductName, message
        );
    }

public slots:
    void doAction() { execute(); }
};
